<?php
namespace Dashboard\Controller;
class PiController extends BaseController{
    public function index(){
        $a        = (int)(I("currpage"));//跳转页数
        $sou      = I('sou');
        $pagesize = 20;
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $table = "goods_zhu";
        $filed = "a.*,b.name as type_name,c.name as shou_name";
        $where = "a.status = 1";
        $join = array("a left join __MEI_TYPE__ b on a.type_three_id = b.id","left join __KE_TYPE__ c on a.shou_type = c.id");
        if($sou){
            $content = I('content');
            $totalpage = 1;
            $currpage  = 1;
            if($sou == 1){
                $list      = joinss($table,$filed,$join,"{$where} and b.name like '%{$content}%'","a.id desc");
            }
            if($sou == 2){
                $list      = joinss($table,$filed,$join,"{$where} and a.name like '%{$content}%'","a.id desc");
            }
            if($sou == 3){
                $list      = joinss($table,$filed,$join,"{$where} and a.title like '%{$content}%'","a.id desc");
            }
            if($sou == 4){
                $list      = joinss($table,$filed,$join,"{$where} and a.money = {$content}","a.id desc");
            }
            if($sou == 5){
                $list      = joinss($table,$filed,$join,"{$where} and a.nums = {$content}","a.id desc");
            }
        }else{
            $result = currpages($table,$filed,$join,$where,"a.id desc",$pagesize,$currpage);
            $totalpage = $result['totalpage'];
            $list      = $result['list'];
            if($result['currpage']){
                $currpage = $result['currpage'];
            }
        }
        if($list){
            foreach ($list as $k=>$v){
                $teacher = sel("teacher","name","id = {$v['teacher_id']}");
                $list[$k]['teacher'] = $teacher['name'];
                //视频课程
                $vedios = sels("goods","","goods_zhu_id = {$v['id']}");
                if($vedios){
                    $list[$k]['vedios'] = $vedios;
                }
                $one = sel("mei_type","name","id = {$v['type_one_id']}");
                $two = sel("mei_type","name","id = {$v['type_two_id']}");
                $list[$k]['type_name'] = $one['name']."/".$two['name']."/".$v['type_name'];
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $covers_shu = sel("covers_shu","nums_seven");
        $this->assign("covers_shu",$covers_shu);
        $id = I('id');
        $currpage = I('currpage');
        $this->assign("currpage",$currpage);
        //商品分类信息
        $mei_type  = sels("mei_type","id,name","pid = 0 and id != 1 and id != 4");
        if($id){
            $list = sel("goods_zhu"," * ",array("id"=>$id));
            $this->assign("list",$list);
            if($list['type_one_id']){
                $type_two_id   = sels("mei_type","id,name","pid = {$list['type_one_id']}");
                $type_three_id = sels("mei_type","id,name","pid = {$list['type_two_id']}");
                $this->assign("type_two_id",$type_two_id);
                $this->assign("type_three_id",$type_three_id);
            }
        }
        //首页分类
        $shou_type = sels("ke_type","id,name","id != 3");
        //教师
        $teacher = sels("teacher","id,name");
        $this->assign("mei_type",$mei_type)->assign("teacher",$teacher)->assign("shou_type",$shou_type);
        $this->display();
    }
    //分类显示
    public function sou(){
        $goods_type = I('goods_type');
        $type = sels("mei_type","id,name","pid = {$goods_type}");
        if(!$type){$type = 1;}else{
            $types = sels("mei_type","id,name","pid = {$type[0]['id']}");
            if(!$types){$types = 1;}
        }
        $this->ajaxReturn(array("info"=>$type,"infos"=>$types));
    }
    public function sous(){
        $goods_type = I('goods_type');
        $type = sels("mei_type","id,name","pid = {$goods_type}");
        if(!$type){$type = 1;}
        $this->ajaxReturn(array("infos"=>$type));
    }
    public function add()
    {
        $teacher_id = I("teacher_id");
        $type_one_id = I("goods_one");
        $type_two_id = I("goods_two");
        $type_three_id = I("goods_three");
        $shou_type    = I("shou_type");
        if ($teacher_id && $type_one_id && $type_two_id && $type_three_id && $shou_type) {
            $data['teacher_id'] = $teacher_id;
            $data['type_one_id'] = $type_one_id;
            $data['type_two_id'] = $type_two_id;
            $data['type_three_id'] = $type_three_id;
            $data['shou_type'] = $shou_type;
            $data['title'] = I('title');
            $data['moneys'] = I('moneys');
            $data['shared'] = I('shared');
            $data['poster'] = I('poster');
            $data['xiao'] = I('xiao');
            $data['covers'] = I('covers');
            $data['intro'] = I('intro');
            $data['nums'] = I('nums');
            $data['tui'] = I('tui');
            $data['jing'] = I('jing');
            $data['status'] = 1;
            $id = I('id');
            //处理图片
            if ($_FILES['one']['name'] != null) {
                $img = I('imgs');
                unlink($img);
                $info = $this->imgUpload("goods/");
                $savename = $info['one']['savename'];
                $savepath = $info['one']['savepath'];
                $data['img'] = "Data/" . $savepath . $savename;
            }
            if ($id) {
                $data['up_at'] = time();
                $res = up_one("goods_zhu","id = {$id}",$data);
                if($res){
                    $this->success("修改成功",U("index"));
                }else{
                    $this->success("修改成功",U("form"));
                }
            }else{
                $data['create_at'] = time();
                $res = ins("goods_zhu",$data);
                if($res){
                    $this->success("添加成功",U("form"));
                }else{
                    $this->success("添加失败",U("form"));
                }
            }
        } else {
            $this->success("请完善课程所属教师或所属分类或课程类型");
        }
    }

    //添加课程
    public function forms(){
        $ke_id = I("ke_id");  //总课程的id
        $id    = I("id");//总课程下的分课程id
        $this->assign("ke_id",$ke_id);
        if($id){
            $list = sel("goods","",array("id"=>$id));
            $this->assign("list",$list);
        }
        $this->display();
    }
    public function add_forms(){
        //处理图片
        if($_FILES['two']['name'] != null){
            $img = I('imgs');
            unlink($img);
            $info                   = $this->imgUpload("video/");
            $savename               = $info["two"]['savename'];
            $savepath               = $info["two"]['savepath'];
            $data['vedios']        = "Data/".$savepath.$savename;
        }
        $data['url']      = I("url");
        $data['ke_time'] = I("ke_time");
        $data['title']   = I("title");
        $data['status']  = I("status");
        $ke_id            = I("ke_id");
        if($ke_id){
            $data['goods_zhu_id'] = $ke_id;
        }
        $id = I('id');

        if ($id) {
            $data['up_at'] = time();
            $res = up_one("goods","id = {$id}",$data);
            if($res){
                $this->success("修改成功",U("index"));
            }else{
                $this->success("修改失败",U("index"));
            }
        }else{
            $data['create_at'] = time();
            $res = ins("goods",$data);
            if($res){
                $this->success("添加成功",U("forms",array("ke_id"=>$ke_id)));
            }else{
                $this->success("添加失败",U("forms",array("ke_id"=>$ke_id)));
            }
        }
    }
    //首页分类
    //类别
    public function ty()
    {
        $list   = sels("ke_type");
        $this->assign("list",$list);
        $this->display();
    }
    public function ty_add(){
        $id = I("id");
        if($id){
            $list = sel("ke_type","","id={$id}");
            $this->assign("list",$list);
        }
        $this->display();
    }
    public function checks()
    {
        $id = I('id');
        $data['name'] = I("name");
        if($_FILES['one']['name'] != null){
            $zhu = I('img');unlink($zhu);
            //处理图片
            $info                   = $this->imgUpload("ke_type/");
            $savename               = $info['one']['savename'];
            $savepath               = $info['one']['savepath'];
            $data['img']    = "Data/".$savepath.$savename;
        }
        if (!$id) {
            $data['create_at'] = time();
            $res = ins("ke_type", $data);
            $this->success("操作成功",U('ty_add'));
        } else {
          $data['up_at'] = time();
            $res = up_one("ke_type", array("id" => $id), $data);
            $this->success("操作成功",U('ty'));
        }
    }
}